//
//  HiOptISystem.m
//  blackjack
//
//  Created by Thi Nguyen Tran on 3/17/10.
//  Copyright 2010 Epsilon. All rights reserved.
//

#import "HiOptISystem.h"
#import "Description.h"
#import "CardCountConstants.h"
#import "CountSystemHelp.h"

@interface HiOptISystem (Private)

int numberOfCount_;
int sumOfResult_;
int trace_[1000];
int undo_;
float result_;

- (void)changeLabel;
- (NSString *)getAdviceForSituation;

@end


@implementation HiOptISystem

@synthesize delegate;

- (IBAction)hiButtonTouched {
	sumOfResult_--;
	
	trace_[undo_] = +1;
	undo_++;
	[self changeLabel];
	
}
- (IBAction)loButtonTouched {
	sumOfResult_++;
	numberOfCount_++;
	
	trace_[undo_] = -1;
	undo_++;
	[self changeLabel];
}
- (IBAction)undoButtonTouched {
	if (numberOfCount_ != 0)
	{
		numberOfCount_ --;
		undo_--;
		if(undo_ < 0) {
			undo_ = 0;
			return;
		}
		if(trace_[undo_] == NEWDECK_TOUCHED) {
			remainDeck_++;
			if(remainDeck_ > MAX_DECKS) remainDeck_ = 1;
			return;
		}
		sumOfResult_ += trace_[undo_];
	}
	[self changeLabel];
}
- (IBAction)newdeckButtonTouched {
	remainDeck_--;
	if(remainDeck_ == 0) {
		remainDeck_ = MAX_DECKS;
	}
	trace_[undo_] = NEWDECK_TOUCHED;
	undo_++;
	[self changeLabel];	
}

- (IBAction)backButtonTouched {
	[delegate backToMainMenu];
}

- (IBAction)musicBtnTouched {
	[delegate viewMusicPicker];
}
- (IBAction)infoBtnTouched
{
	Description *desc = [[Description alloc] init];
	[desc setDelegate:self];
	[self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
	[self presentModalViewController:desc animated:YES];
	[desc showDescriptionFor:HIOPTI_SYSTEM_NAME];
	[desc release];
}
- (IBAction)helpBtnTouched {
	CountSystemHelp * helpScene = [[CountSystemHelp alloc] init];
	[helpScene setDelegate:self];
	[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
	[self presentModalViewController:helpScene animated:YES];
	[helpScene release];
}
- (void)viewDidLoad {
	NSLog(@"reset content called");
	[super viewDidLoad];
	numberOfCount_ = 0;
	sumOfResult_ = 0;
	remainDeck_ = MAX_DECKS;
	undo_ = 0;
	[self changeLabel];
}
- (void)changeLabel
{
	[countingResult setText:[NSString stringWithFormat:@"%.1f", (float)sumOfResult_/(float)remainDeck_]];
	[numberCounting setText:[NSString stringWithFormat:@"%d", sumOfResult_]];NSString * str = [self getAdviceForSituation];
	[advice_ setText:str];
}

- (NSString *)getAdviceForSituation {
	float true_count_val = (float)sumOfResult_/(float)remainDeck_;
	if(true_count_val < 1.0f) {
		return BET_ADVICE_1;
	}
	else if(true_count_val < 3.0f) {
		return BET_ADVICE_2;
	}
	else if(true_count_val < 5.0f) {
		return BET_ADVICE_3;
	}
	else if(true_count_val < 7.0f) {
		return BET_ADVICE_4;
	}
	else {
		return BET_ADVICE_5;
	}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [super dealloc];
}


@end
